﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Facebook;
using System.Windows.Media.Imaging;

namespace FBEventMaker.Facebook
{
    public partial class FacebookInfoPage : PhoneApplicationPage
    {
        private string mAccessToken;
        private IDictionary<string, object> mMyFBInfo;

        public FacebookInfoPage()
        {
            InitializeComponent();

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            // Query parameter passed in the invokation of this page
            mAccessToken = NavigationContext.QueryString["access_token"];
            // remove the log-in page from the navigation stack
            NavigationService.RemoveBackEntry();
        }


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            FacebookClient fb = new FacebookClient(mAccessToken);

            #region define service routine
            fb.GetCompleted += (o, args) =>
            {
                if (args.Error == null)
                {
                    mMyFBInfo = (IDictionary<string, object>)args.GetResultData();

                    // fill out information on the phone page
                    Dispatcher.BeginInvoke(
                        () =>
                        {
                            picProfile.Source = new BitmapImage(new Uri(string.Format("https://graph.facebook.com/{0}/picture", mMyFBInfo["id"])));

                            ProfileName.Text = mMyFBInfo["name"] as string;
                            Uid.Text = mMyFBInfo["id"] as string;
                            Gender.Text = mMyFBInfo["gender"] as string;
                            Locale.Text = mMyFBInfo["locale"] as string;
                        });
                }
                else
                {
                    Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message));
                }
            };
            #endregion

            // do a GetAsync me in order to get basic details of the user.
            fb.GetAsync("me");

            FqlSample();
        }
        
        // single query request
        private void FqlSample()
        {
            var fb = new FacebookClient(mAccessToken);

             // query to get all the friends
             var query1 = string.Format("SELECT id, name, gender, locale FROM user WHERE id = me()");
           

            // call the Query or QueryAsync method to execute a single fql query.
             fb.QueryAsync(query1);
        
        }
               
        private void CreateEvent_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Facebook/FacebookEventPage.xaml?access_token=" +mAccessToken, UriKind.Relative));
        }

        private void m_ViewWallPost_click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Facebook/ViewWallPost.xaml?access_token=" +mAccessToken, UriKind.Relative));
        }
    }
}